'''
少儿健康医疗保险
'''

import sys
sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests, json
class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "少儿健康医疗保险"

    # 生产产品信息
    def genProductInfo(self):

        plan = {

            "planA": {
                # 年龄段 0-18
                "year": [str(i) for i in range(0, 19)],
                # 意外身故/伤残保险金
                "accidentDeath": ["100000"],
                # 住院医疗保险金
                "hospitalMedical": ["100000"],
                # 传染病门诊急诊、住院医疗
                "contagionMedical": ["10000"],
                # 特定传染病保险金
                "specialMedical": ["30000"],
                # 意外住院保险金
                "accidentHospital": ["60000"],
                # 意外门诊保险金
                "accidentOutpatient": ["5000"],
                # 意外住院津贴
                "accidentAllowance": ["100"],
                # 计划
                "plan": ["豪华版"],
                # 计划代码
                "planCode": ["0"]
            },
            "planB": {
                # 年龄段 0-18
                "year": [str(i) for i in range(0, 19)],
                # 意外身故/伤残保险金
                "accidentDeath": ["50000"],
                # 住院医疗保险金
                "hospitalMedical": ["60000"],
                # 传染病门诊急诊、住院医疗
                "contagionMedical": ["5000"],
                # 特定传染病保险金
                "specialMedical": ["10000"],
                # 意外住院保险金
                "accidentHospital": ["20000"],
                # 意外门诊保险金
                "accidentOutpatient": ["5000"],
                # 意外住院津贴
                "accidentAllowance": ["100"],
                # 计划
                "plan": ["热销款"],
                # 计划代码
                "planCode": ["1"]
            },
        }

        for k in plan:

            for v in product(

                    plan[k]["year"], plan[k]["accidentDeath"], plan[k]["hospitalMedical"], plan[k]["contagionMedical"],
                    plan[k]["specialMedical"], plan[k]["accidentHospital"], plan[k]["accidentOutpatient"],
                    plan[k]["accidentAllowance"], plan[k]["plan"], plan[k]["planCode"]

            ):


                self.mysql.cursor.execute(
                    domain[self.name]["insert_sql"].format(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]))
                self.mysql._conn.commit()

                print(
                    domain[self.name]["insert_sql"].format(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
                )

        #特殊
        special = {

            "planA":{

                # 年龄段 0-18
                "year": [str(i) for i in range(0, 19)],
                # 意外身故/伤残保险金
                "accidentDeath": ["50000","100000"],
                # 住院医疗保险金
                "hospitalMedical": ["0"],
                # 传染病门诊急诊、住院医疗
                "contagionMedical": ["0"],
                # 特定传染病保险金
                "specialMedical": ["0"],

                # 意外住院保险金
                "accidentHospital": ["0"],
                # 意外门诊保险金
                "accidentOutpatient": ["0"],
                # 意外住院津贴
                "accidentAllowance": ["0","100"],
                # 计划
                "plan": ["自定义"],
                # 计划代码
                "planCode": ["2"]

            },

            "planB": {

                # 年龄段 0-18
                "year": [str(i) for i in range(0, 19)],
                # 意外身故/伤残保险金
                "accidentDeath": ["50000", "100000"],
                # 住院医疗保险金
                "hospitalMedical": ["60000", "100000"],
                # 传染病门诊急诊、住院医疗
                "contagionMedical": ["5000"],
                # 特定传染病保险金
                "specialMedical": ["10000"],

                # 意外住院保险金
                "accidentHospital": ["0"],
                # 意外门诊保险金
                "accidentOutpatient": ["0"],
                # 意外住院津贴
                "accidentAllowance": ["0", "100"],
                # 计划
                "plan": ["自定义"],
                # 计划代码
                "planCode": ["2"]

            },


            "planC": {

                # 年龄段 0-18
                "year": [str(i) for i in range(0, 19)],
                # 意外身故/伤残保险金
                "accidentDeath": ["50000", "100000"],
                # 住院医疗保险金
                "hospitalMedical": ["60000","100000"],
                # 传染病门诊急诊、住院医疗
                "contagionMedical": ["5000"],
                # 特定传染病保险金
                "specialMedical": ["10000"],

                # 意外住院保险金
                "accidentHospital": ["20000","60000"],
                # 意外门诊保险金
                "accidentOutpatient": ["5000"],
                # 意外住院津贴
                "accidentAllowance": ["0", "100"],
                # 计划
                "plan": ["自定义"],
                # 计划代码
                "planCode": ["2"]

            },
            "planD": {

                # 年龄段 0-18
                "year": [str(i) for i in range(0, 19)],
                # 意外身故/伤残保险金
                "accidentDeath": ["50000", "100000"],
                # 住院医疗保险金
                "hospitalMedical": ["60000", "100000"],
                # 传染病门诊急诊、住院医疗
                "contagionMedical": ["10000"],
                # 特定传染病保险金
                "specialMedical": ["30000"],
                # 意外住院保险金
                "accidentHospital": ["20000", "60000"],
                # 意外门诊保险金
                "accidentOutpatient": ["5000"],
                # 意外住院津贴
                "accidentAllowance": ["0", "100"],
                # 计划
                "plan": ["自定义"],
                # 计划代码
                "planCode": ["2"]

            }

        }

        for k in special:

            for v in product(

                    special[k]["year"], special[k]["accidentDeath"], special[k]["hospitalMedical"], special[k]["contagionMedical"],
                    special[k]["specialMedical"], special[k]["accidentHospital"], special[k]["accidentOutpatient"],
                    special[k]["accidentAllowance"], special[k]["plan"], special[k]["planCode"]

            ):


                self.mysql.cursor.execute(
                    domain[self.name]["insert_sql"].format(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]))
                self.mysql._conn.commit()

                print(
                    domain[self.name]["insert_sql"].format(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9])
                )


    # 执行爬虫
    def requestJson(self, dt=None):
        try:
            # 豪华款
            if dt["planCode"] == "0":

                data = {"planClass": 0, "amountFormList": [
                    {"suddennessDie": 100000, "outpatientMedic": 5000, "hospitalMedic": 60000, "hospitalAllowance": 100,
                     "vaccineMedic": 100000, "noVaccineMedic": 100000, "infectiousDiseaseMedic": 10000,
                     "infectiousDiseaseHospitalMedic": 10000, "specialInfectiousDisease": 30000},
                    {"suddennessDie": 50000, "outpatientMedic": 5000, "hospitalMedic": 20000, "hospitalAllowance": 100,
                     "vaccineMedic": 60000, "noVaccineMedic": 60000, "infectiousDiseaseMedic": 5000,
                     "infectiousDiseaseHospitalMedic": 5000, "specialInfectiousDisease": 10000},
                    {"suddennessDie": "50000", "suddennessDisability": "50000", "hospitalMedic": "0",
                     "outpatientMedic": "0", "hospitalAllowance": "0", "vaccineMedic": "0",
                     "specialInfectiousDisease": "0", "infectiousDiseaseHospitalMedic": "0",
                     "infectiousDiseaseMedic": "0"}], "beginDate": "2020-01-14", "insurantNum": "1",
                 "birthDate": "{year}-01-10".format( year = str( 2019-int(dt["year"])))}

                r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"],
                                  data=json.dumps(data))
                print("豪华版", data)
                print(r.text)
                if r.status_code == 200:
                    price = r.json()["quoteResult1"]["toaUserPremium"]

                    data = {

                        "name": self.name,
                        "id": dt["id"],
                        "price": price
                    }
                    self.mysql.updatePrice(data)

            elif dt["planCode"] == "1":

                data = {"planClass": dt["planCode"], "amountFormList": [
                    {"suddennessDie": 100000, "outpatientMedic": 5000, "hospitalMedic": 60000, "hospitalAllowance": 100,
                     "vaccineMedic": 100000, "noVaccineMedic": 100000, "infectiousDiseaseMedic": 10000,
                     "infectiousDiseaseHospitalMedic": 10000, "specialInfectiousDisease": 30000},
                    {"suddennessDie": 50000, "outpatientMedic": 5000, "hospitalMedic": 20000, "hospitalAllowance": 100,
                     "vaccineMedic": 60000, "noVaccineMedic": 60000, "infectiousDiseaseMedic": 5000,
                     "infectiousDiseaseHospitalMedic": 5000, "specialInfectiousDisease": 10000},
                    {"suddennessDie": "100000", "suddennessDisability": "100000", "hospitalMedic": "60000",
                     "outpatientMedic": "5000", "hospitalAllowance": "100", "vaccineMedic": "60000",
                     "specialInfectiousDisease": "10000", "infectiousDiseaseHospitalMedic": "5000",
                     "infectiousDiseaseMedic": "5000"}], "beginDate": "2020-01-13", "insurantNum": "1",
                        "birthDate": "{year}-01-01".format(year=str(2019 - int(dt["year"])))}

                r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"],
                                  data=json.dumps(data))
                print("热销款", data)
                print(r.text)
                if r.status_code == 200:
                    price = r.json()["quoteResult2"]["toaUserPremium"]

                    data = {

                        "name": self.name,
                        "id": dt["id"],
                        "price": price
                    }
                    self.mysql.updatePrice(data)

            else:

                data = {"planClass": dt["planCode"], "amountFormList": [
                    {"suddennessDie": 100000, "outpatientMedic": 5000, "hospitalMedic": 60000, "hospitalAllowance": 100,
                     "vaccineMedic": 100000, "noVaccineMedic": 100000, "infectiousDiseaseMedic": 10000,
                     "infectiousDiseaseHospitalMedic": 10000, "specialInfectiousDisease": 30000},
                    {"suddennessDie": 50000, "outpatientMedic": 5000, "hospitalMedic": 20000, "hospitalAllowance": 100,
                     "vaccineMedic": 60000, "noVaccineMedic": 60000, "infectiousDiseaseMedic": 5000,
                     "infectiousDiseaseHospitalMedic": 5000, "specialInfectiousDisease": 10000},

                    # 意外身故/伤残保险金
                    {"suddennessDie": dt["accidentDeath"],
                     # 意外身故/伤残保险金
                     "suddennessDisability": dt["accidentDeath"],
                     # 意外住院保险金
                     "hospitalMedic": dt["accidentHospital"],
                     # 意外门诊保险金
                     "outpatientMedic": dt["accidentOutpatient"],
                     # 意外住院津贴
                     "hospitalAllowance": dt["accidentAllowance"],
                     # 住院医疗保险金
                     "vaccineMedic": dt["hospitalMedical"],
                     # 特定传染病保险金
                     "specialInfectiousDisease": dt["specialMedical"],
                     # 传染病门诊急诊、住院医疗
                     "infectiousDiseaseHospitalMedic": dt["contagionMedical"],
                     # 传染病门诊急诊、住院医疗
                     "infectiousDiseaseMedic": dt["contagionMedical"]}
                ], "beginDate": "2020-01-13", "insurantNum": "1",
                        "birthDate": "{year}-01-01".format(year=str(2019 - int(dt["year"])))}

                print("自定义", data)
                r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"],
                                  data=json.dumps(data))
                print(r.text)

                if r.status_code == 200:
                    price = r.json()["quoteResult3"]["toaUserPremium"]

                    data = {

                        "name": self.name,
                        "id": dt["id"],
                        "price": price
                    }
                    self.mysql.updatePrice(data)

        except Exception as e:

            data = {

                "name": self.name,
                "id": dt["id"],
                "state": 3

            }
            print("流程不规范更新失败:%d" % dt["id"])
            self.mysql.updateTask(data)

    def main(self):

        while True:

            # 随机领取一条数据
            dt = self.mysql.randTask(self.name)

            print(dt)

            if not dt:
                print("产品价格抓取完成~~~")

                return

                # 任务执行状态
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 1
            }
            self.mysql.updateTask(data)

            self.requestJson(dt)
